package com.isman.placeslist;

import com.isman.placeslist.apiobject.Session;
import com.isman.placeslist.setting.ProjectPref;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class PageMain extends Activity {
	public static final int SINGIN_SINGUP = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_main);

		init();
		initData();
		mCheckSession();
	}

	protected void init() {
		vgridList = (GridView) findViewById(R.id.gvw_main_list);
		vibtnSearch = (ImageButton) findViewById(R.id.ibtn_main_search);
		vibtnImage = (ImageButton) findViewById(R.id.ibtn_main_carmera);

		vibtnImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(IMAGE_CHOINE);
			}
		});
		vibtnSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(PageMain.this, PageSearchSort.class));
			}
		});
	}

	protected void initData() {
		vgridList.setAdapter(new ImageAdapter(getApplicationContext()));
	}

	protected void mCheckSession() {
		Session session = new Session();
		if (!ProjectPref.restoreSession(session, this)) {
			startActivityForResult(new Intent(this, PageChoine.class),
					SINGIN_SINGUP);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case SINGIN_SINGUP:
				Session mSingIn = (Session) data.getExtras().getSerializable(
						SINGIN_INFO);
				ProjectPref.saveSession(mSingIn, this);
				break;
			default:
				break;
			}
		} else {
			this.finish();
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder;
		switch (id) {
		case IMAGE_CHOINE:
			final CharSequence[] items = { "Gallery", "Camera" };
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Selected image from.");
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					Toast.makeText(getApplicationContext(), items[item],
							Toast.LENGTH_SHORT).show();
				}
			});
			return builder.create();
		case SINGOUT_DIALOG:
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Selected image from.");
			builder.setPositiveButton("Singout",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub

						}
					});
			builder.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
			return builder.create();
		}
		return null;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
		case R.id.itmReload:
			break;
		case R.id.itmAccount:
			intent = new Intent(this, PerfAccount.class);
			startActivity(intent);
			break;
		case R.id.itmSetting:
			intent = new Intent(this, PerfSetting.class);
			startActivity(intent);
			break;
		case R.id.itmSingOut:
			ProjectPref.clear(this);
			intent = new Intent(this, PageChoine.class);
			startActivityForResult(intent, SINGIN_SINGUP);
			break;
		}
		return true;
	}

	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mLabels.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// ImageView imageView;
			// if (convertView == null) {
			// imageView = new ImageView(mContext);
			// imageView.setLayoutParams(new GridView.LayoutParams(
			// GridView.LayoutParams.MATCH_PARENT, 120));
			// imageView.setAdjustViewBounds(false);
			// imageView.setScaleType(ImageView.ScaleType.FIT_START);
			// imageView.setPadding(8, 8, 8, 8);
			// } else {
			// imageView = (ImageView) convertView;
			// }
			//
			// imageView.setImageResource(mThumbIds[position]);

			View view = LayoutInflater.from(mContext).inflate(
					R.layout.inc_item_icon, null);
			TextView txtLabel = (TextView) view
					.findViewById(R.id.lbl_icon_label);
			txtLabel.setText(mLabels[position]);
			
			final Session ms = new Session();
			ProjectPref.restoreSession(ms, PageMain.this);

			view.setOnClickListener(new OnClickListener() {
				Intent intent;
				@Override
				public void onClick(View v) {
					switch (position) {
					case 0:
						intent = new Intent(PageMain.this, PageSearchSort.class);
						startActivity(intent);
						break;
					case 1:
						intent = new Intent(getApplicationContext(),
								PageNewStatus.class);
						startActivity(intent);
						break;
					case 2:
						intent = new Intent(PageMain.this, PagePeople.class);
						startActivity(intent);
						break;
					case 3:

						intent = new Intent(PageMain.this, PageUser.class);
						intent.putExtra(PageUser.USER_ID, ms.getId());
						startActivity(intent);
						break;
					case 4:
						Toast.makeText(mContext, mLabels[position], Toast.LENGTH_LONG);
						break;
					default:
						break;
					}
				}
			});
			return view;
		}

		private Context mContext;

		private String[] mLabels = { "Places", "New Status", "Friends",
				"Profile", "Recommend" };
	}

	private static final int IMAGE_CHOINE = 0;
	private static final int SINGOUT_DIALOG = 1;
	protected static final String SINGIN_INFO = "SinginInfo";
	private GridView vgridList;
	private ImageButton vibtnSearch;
	private ImageButton vibtnImage;
}